<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/30
 * Time: 16:00
 */
namespace Controller;

class ShopController extends TooController {
    //分类管理
    public function system_category(){
        //是否有id参数,实现批量删除
        if($_GET['id']){
            $id=trim($_GET['id'],','); //传递过来的参数多了一个 , 号
            if($this->pdo->pdo_del('s_class'," id in ($id)")){
                $this->retuAjax(array('type'=>1,'msg'=>'删除成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'删除失败'));
            }

        }

        $arr=$this->pdo->pdo_sel("select * from s_class order by sort desc",2);
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //添加分类
    public function system_category_add(){
        if($_POST){
            $arr=array();
            $arr['title']=$_POST['title'];
            $arr['sort']=$_POST['sort'];

            if($this->pdo->pdo_ins($arr,'s_class')){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
        $this->view->display();
    }

    //删除分类
    public function system_category_del(){
        $id=$_GET['id'];
        if($id){
            if($this->pdo->pdo_del("s_class"," id = $id")){
                $this->retuAjax(array('type'=>'1','msg'=>'删除成功'));
            }else{
                $this->retuAjax(array('type'=>'2','msg'=>'删除失败'));
            }
        }else{
            $this->retuAjax(array('type'=>'2','msg'=>'非法操作'));
        }
    }

    //修改分类 --- 修改和添加使用一个页面
    public function system_category_edit(){
        if($_POST){
            $id=$_POST['id'];
            $arr=array();
            $arr['title']=$_POST['title'];
            $arr['sort']=$_POST['sort'];
            if($this->pdo->pdo_upd($arr,'s_class'," id = $id")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作成功'));
            }
        }
        $id=$_GET['id'];
        $list=$this->pdo->pdo_sel("select * from s_class where id = $id");
        $this->view->assign('list',$list);
        $this->view->display('system_category_add');
    }


    //商品管理
    public function goods(){
        $sql="select * from s_goods order by sort desc";
        $arr=$this->pdo->pdo_sel($sql,2);
        $this->view->assign('arr',$arr);

        $this->view->display();
    }

    //添加商品
    public function goods_add(){
        if($_POST){
           //处理基本数据
            $arr=array();
            $arr['img']=$_POST['img'];
            $arr['name']=$_POST['name'];
            $arr['kc']=$_POST['kc'];
            $arr['msg']=$_POST['msg'];
            $arr['txt']= htmlspecialchars($_POST['editorValue']);
            $arr['start']= $_POST['start'];
            $arr['money']= $_POST['money'];
            $arr['f_id']= $_POST['f_id'];
            $arr['l_id']= $_POST['l_id'];
            $arr['sort']= $_POST['sort'];
            //添加基本数据
            $id=$this->pdo->pdo_ins($arr,'s_goods');

            //存入规格数据
            if($id){
                $sql="insert into s_goods_g(gname,gmoney,g_id) values ";
                for($i=0;$i<count($_POST['gname']);$i++){
                    $sql.="('{$_POST['gname'][$i]}','{$_POST['gmoney'][$i]}',$id),";
                }
                $sql=trim($sql,',');
                if($this->pdo->pdo_query_i($sql)){
                    $this->success('成功'); exit;
                };
            }
            $this->success('失败'); exit;
        }

        $fl=$this->pdo->pdo_sel("select * from s_class order by sort desc",2);
        $this->view->assign('fl',$fl);
        $lm=$this->pdo->pdo_sel("select * from s_lm order by sort desc",2);
        $this->view->assign('lm',$lm);

        $this->view->display();
    }

    //商品修改---上架下架
    public function goods_upd(){
        $id=$_GET['id'];
        $type=$_GET['type']=='xj'?'2':'1'; //得到修改值
        if($id){
            $this->pdo->pdo_upd(array('start'=>$type),'s_goods'," id = $id");
            $this->retuAjax(array('type'=>1));
        }else{
            $this->retuAjax(array('type'=>'2'));
        }
    }

    //商品删除
    public function goods_del(){
        $id=trim($_GET['id'],','); //去掉首位 ,号 多个删除的时候会多一个,号
        if($id){
            if($this->pdo->pdo_del('s_goods'," id in($id)")){
                $this->retuAjax(array('type'=>1));
            }else{
                $this->retuAjax(array('type'=>2));
            }
        }else{
            $this->retuAjax(array('type'=>2));
        }
    }

    //商品修改
    public function goods_edit(){
        if($_POST){
            //处理基本数据
            $arr=array();
            //判断是否修改了图片
            if(!empty($_POST['img'])){
                $arr['img']=$_POST['img'];
            }

            $arr['name']=$_POST['name'];
            $arr['kc']=$_POST['kc'];
            $arr['msg']=$_POST['msg'];
            $arr['txt']= htmlspecialchars($_POST['editorValue']);
            $arr['start']= $_POST['start'];
            $arr['money']= $_POST['money'];
            $arr['f_id']= $_POST['f_id'];
            $arr['l_id']= $_POST['l_id'];
            $arr['sort']= $_POST['sort'];
            $id=$_POST['id'];
            //添加基本数据
           $this->pdo->pdo_upd($arr,'s_goods'," id = '{$id}'");


            //规格的修改---把正确的规格删除,在重新添加
            $this->pdo->pdo_del("s_goods_g"," g_id = $id");

            $sql="insert into s_goods_g(gname,gmoney,g_id) values ";
            for($i=0;$i<count($_POST['gname']);$i++){
                $sql.="('{$_POST['gname'][$i]}','{$_POST['gmoney'][$i]}',$id),";
            }
            $sql=trim($sql,',');
            if($this->pdo->pdo_query_i($sql)){
                $this->success('成功','Shop','goods_edit&id='.$id); exit;
            };

            $this->success('失败','Shop','goods_edit&id='.$id); exit;
        }
        $id=$_GET['id'];
        //修改商品信息
        $arr=$this->pdo->pdo_sel("select * from s_goods where id = $id");
        $this->view->assign('arr',$arr);
        //对应规格信息
        $garr=$this->pdo->pdo_sel("select * from s_goods_g where g_id = $id",2);
        $this->view->assign('garr',$garr);

        //分类信息
        $fl=$this->pdo->pdo_sel("select * from s_class order by sort desc",2);
        $this->view->assign('fl',$fl);
        $lm=$this->pdo->pdo_sel("select * from s_lm order by sort desc",2);
        $this->view->assign('lm',$lm);


        $this->view->display('goods_add');
    }
}